package com.jg.xmlSchema2JsonSchema.xsd;

import cn.hutool.core.collection.CollectionUtil;
import com.jg.xmlSchema2JsonSchema.xsd.util.XsdUtil;
import org.w3c.dom.Element;

import java.util.List;

/**
 * @author czg
 */
public class XsAll {
    // 节点描述
    private String annotation;
    private Element element;
    private String id;
    private String maxOccurs;
    private String minOccurs;

    public XsAll(Element element) {
        this.element = element;
        getData();
    }

    private void getData() {
        XsAnnotation xsAnnotation = XsdUtil.getAnnotation(element);
        if (xsAnnotation != null) {
            annotation = xsAnnotation.getText();
        }
        maxOccurs = element.getAttribute("maxOccurs");
        minOccurs = element.getAttribute("minOccurs");
        id = element.getAttribute("id");

    }

    public String getAnnotation() {
        return annotation;
    }

    public String getId() {
        return id;
    }

    public String getMinOccurs() {
        return minOccurs;
    }

    public String getMaxOccurs() {
        return maxOccurs;
    }

    public XsElement getFirstElement() {
        List<XsElement> els = getElements();
        if (CollectionUtil.isNotEmpty(els)) {
            return els.get(0);
        }
        return null;
    }

    public List<XsElement> getElements() {
        return XsdUtil.getElements(element);
    }

    public XsElement getElement(String name) {
        List<XsElement> elements = XsdUtil.getElements(element);
        for (XsElement xa : elements) {
            if (xa.getName().equals(name)) {
                return xa;
            }
        }
        return null;
    }

    public Element getXmlElement() {
        return element;
    }
}
